#include "thread.h"

int main(int argc,char *argv[])
{
    ARGS_CHECK(argc,4);

    threadPool_t thread;
    int ret=initThreadPool(&thread,atoi(argv[3]));
    ERROR_CHECK(ret,-1,"makeWorker");
    
    makeWork(&thread);

    int sockfd;
    ret=setTCP(argv[1],argv[2],&sockfd);
    ERROR_CHECK(ret,-1,"setTCP");

    int epfd=epoll_create(10);
    epollAdd(epfd,sockfd);
    ERROR_CHECK(ret,-1,"epollAdd");

    struct epoll_event rdset[10];
    
    while(1){
        int rdNum=epoll_wait(epfd,rdset,10,-1);

        for(int i=0;i<rdNum;i++){
            if(rdset[i].data.fd==sockfd){
                int netfd=accept(sockfd,NULL,NULL);

                pthread_mutex_lock(&thread.mutex);
                enQueue(&thread.task,netfd);
                pthread_cond_signal(&thread.cond);
                pthread_mutex_unlock(&thread.mutex);


            }
        }

    }

    return 0;
}

